<?php
declare (strict_types=1);

namespace app\listener;

use app\common\model\Transfer;
use app\index\service\TransferableInterface;

class TransferComplete
{
    /**
     * 转账完成
     * @param Transfer $transfer
     * @return void
     */
    public function handle(Transfer $transfer)
    {
        // 转账已完成，完成后续业务逻辑
        $service = $this->getTransferService($transfer->transferTypeClass);
        if (is_null($service)) {
            // TODO 记录错误日志
            return;
        }
        $service->completionTransfer($transfer);
    }

    /**
     * @param $transferTypeClass
     * @return TransferableInterface|null
     */
    private function getTransferService($transferTypeClass): ?TransferableInterface
    {
        return app_instance(TransferableInterface::class, $transferTypeClass);
    }
}
